import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control

// 引入 TinyMCE 核心文件
import tinymce from '../public/tinymce/tinymce.min'
// 引入主题
import '../public/tinymce/themes/silver/theme.min';
// 引入图标
import '../public/tinymce/icons/default/icons.min';

// 引入所需的插件（按需引入）
import '../public/tinymce/plugins/advlist/plugin.min';
import '../public/tinymce/plugins/anchor/plugin.min';
// 可按需添加更多插件

// 若需要中文语言包
import '../public/zh_CN.js';

// 设置 baseURL 为 public 文件夹下 tinymce 目录的路径
tinymce.baseURL = '/tinymce';

// 定义判断权限的函数
function hasPermission(permissions, target) {
  return permissions.includes(target);
}

// 全局注册 v-permission 指令
Vue.directive('permission', {
  inserted: function (el, binding) {
    // 这里应该根据实际情况获取用户权限
    const permissions = ['GET /admin/admin/list', 'POST /admin/admin/create', 'POST /admin/admin/update', 'POST /admin/admin/delete'];
    if (!hasPermission(permissions, binding.value[0])) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  }
});

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
  const { mockXHR } = require('../mock')
  mockXHR()
}

// set ElementUI lang to EN
// Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
// 页面布局默认为 medium
Vue.use(ElementUI,{size:"medium"})

Vue.config.productionTip = false

// 初始化 TinyMCE
Vue.prototype.$tinymce = tinymce;
Vue.directive('tinymce', {
  inserted: function (el) {
    tinymce.init({
      selector: '#tinydemo', //容器，可使用css选择器
      language: 'zh_CN', //调用放在langs文件夹内的语言包
      toolbar: false, //隐藏工具栏
      menubar: false, //隐藏菜单栏
      inline: true, //开启内联模式
      plugins: ['quickbars', 'link', 'table'], //选择需加载的插件
    });
  },
  unbind: function () {
    tinymce.remove();
  }
});

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})